package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListSerivce;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

/**
 * @author 李旭
 * @date 2021/10/13 10:37
 * @Description:
 */
@RestController
@RequestMapping("/api/list")
public class ListApiController {

/*    @Autowired
    private TransportClient transportClient;//低级别客户端   早期*/
    @Autowired
    private RestHighLevelClient restHighLevelClient;//高级别客户端   最近
/*    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;//低级别客户端   早期*/
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;//高级别客户端   最近

    @Autowired
    private ListSerivce listSerivce;

    //创建索引库  Document feild 类型
    @GetMapping("/createIndex")
    public Result createIndex(){
        //创建索引库
        elasticsearchRestTemplate.createIndex(Goods.class);
        //mapping
        elasticsearchRestTemplate.putMapping(Goods.class);//没有问题
        return Result.ok();
    }

    //上架
    @PostMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        listSerivce.onSale(skuId);
        return Result.ok();
    }

    //下架
    @PostMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        listSerivce.cancelSale(skuId);
        return Result.ok();
    }
    //商品热度评分  （加分接口）
    @PutMapping("/inner/incrHotScore/{skuId}/{score}")
    public void incrHotScore(@PathVariable Long skuId,@PathVariable Integer score){
        listSerivce.incrHotScore(skuId,score);

    }
    //开始搜索
    @PostMapping("/inner/search")
    public SearchResponseVo  search(@RequestBody SearchParam searchParam){
        return listSerivce.search(searchParam);
    }
}
